package com.vueapi.controller.web;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping("/zip")
public class ZipController {

    @GetMapping("/combine")
    public ResponseEntity<byte[]> combineAndEncryptFolders() throws Exception {
        // 指定三个要压缩的文件夹路径
        String folderPath1 = "D:\\user_data\\user_1";
        String folderPath2 = "D:\\user_data\\user_2";
        String folderPath3 = "D:\\user_data\\user_3";

        // 设置新的空压缩包的名称
        String combinedZipFileName = "combined_encrypted_zip_" + UUID.randomUUID().toString() + ".zip";

        // 设置解压密码
        String decryptionPassword = "123456";

        // 创建ZipFile对象用于新的空压缩包
        ZipFile combinedZipFile = new ZipFile(combinedZipFileName);

        // 创建压缩参数，包括密码
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 使用标准加密
        zipParameters.setPassword(decryptionPassword);

        // 分别对三个文件夹进行压缩并添加到新的空压缩包
        addFolderToCombinedZip(combinedZipFile, folderPath1, zipParameters);
        addFolderToCombinedZip(combinedZipFile, folderPath2, zipParameters);
        addFolderToCombinedZip(combinedZipFile, folderPath3, zipParameters);

        // 读取新的空压缩包内容
        byte[] combinedZipBytes = org.apache.commons.io.FileUtils.readFileToByteArray(new File(combinedZipFileName));

        // 设置HTTP响应头部以指定文件名和加密类型
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + combinedZipFileName);
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        // 删除临时新的空压缩包文件
        new File(combinedZipFileName).delete();

        // 返回加密的新的空压缩包数据作为响应
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(combinedZipBytes.length)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(combinedZipBytes);
    }

    private void addFolderToCombinedZip(ZipFile combinedZipFile, String folderPath, ZipParameters zipParameters)
            throws Exception {
        // 创建临时文件夹以存储子文件夹的压缩包
        String tempFolderPath = "temp_folder_" + UUID.randomUUID().toString();
        File tempFolder = new File(tempFolderPath);
        tempFolder.mkdir();

        // 创建子文件夹的ZIP文件名
        String subZipFileName = folderPath.substring(folderPath.lastIndexOf(File.separator) + 1) + ".zip";

        // 创建子文件夹的ZIPFile对象
        ZipFile subZipFile = new ZipFile(tempFolderPath + File.separator + subZipFileName);

        // 压缩子文件夹内容
        subZipFile.addFolder(folderPath, zipParameters);

        // 添加子文件夹的ZIP文件到新的空压缩包
        combinedZipFile.addFile(new File(tempFolderPath + File.separator + subZipFileName), zipParameters);

        // 删除临时文件夹及其内容
        org.apache.commons.io.FileUtils.deleteDirectory(tempFolder);
    }
}
